/**
 * 
 */
package com.edocbase.desktop.plugins.mdairy.parts;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;

import com.edocbase.biz.diary.entities.DiaryEntity;
import com.edocbase.desktop.search.DocumentIndexable;

/**
 * @author lifei114@gmail.com
 *
 */
public class DiaryIndexable implements DocumentIndexable {
	public static final String ID_FIELD = "ID";
	public static final String PATH_FIELD = "PATH";
	public static final String TITLE_FIELD = "TITLE";
	public static final String CONTENT_FIELD = "CONTENT";
	public static final String DELETED_FIELD = "DELETED";
	public static final String DELETED = "1";
	public static final String NO_DELETED = "0";
	private DiaryEntity diary = null;
	private Document doc = null;
	private String content = null;
	public DiaryIndexable(DiaryEntity diary,String content) {
		super();
		this.diary = diary;
		this.content = content;
	}

	public DiaryEntity getDiary() {
		return diary;
	}

	public void setDiary(DiaryEntity diary) {
		this.diary = diary;
	}

	/* (non-Javadoc)
	 * @see com.edocbase.desktop.search.DocumentIndexable#getId()
	 */
	@Override
	public String getId() {
		return diary.getDiaryId();
	}

	/* (non-Javadoc)
	 * @see com.edocbase.desktop.search.DocumentIndexable#getDocument()
	 */
	@Override
	public Document getDocument() {
		if (doc == null){
			doc = new Document();
			String id = diary.getDiaryId();
			String path = diary.getCatalogPath();
			String title = diary.getTitle();
			doc.add(new Field(ID_FIELD, id, Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(PATH_FIELD, path, Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(TITLE_FIELD, title, Store.YES, Index.ANALYZED));
			doc.add(new Field(CONTENT_FIELD, content, Store.YES, Index.ANALYZED));
			doc.add(new Field(DELETED_FIELD, NO_DELETED, Store.YES, Index.NOT_ANALYZED_NO_NORMS));
		}
		return doc;
	}

}
